#include<stdio.h>

typedef struct time
{
    int mou;
    int day;
    int sum;
}time[100];

int main(void)
{
    time s1[100];
    int i = 0;
    scanf("%d %d",&s1[0]->mou,&s1[0]->day);
    while (s1[i]->mou != 0 && s1[i]->day != 0)
    {
        s1[i]->sum = (s1[i]->mou)*31+s1[i]->day;
        i++;
        scanf("%d %d",&s1[i]->mou,&s1[i]->day);
    }
    
    int boo = 0;
    int j = 0;
    while (boo == 0)
    {
        boo = 1;
        for (int j = 0; j < i; j++)
        {
            time temp[1];
            if (s1[j]->sum>s1[j+1]->sum)
            {
                temp[0]->day = s1[j]->day;
                temp[0]->mou = s1[j]->mou;
                temp[0]->sum = s1[j]->sum;
                s1[j]->day = s1[j+1]->day;
                s1[j]->mou = s1[j+1]->mou;
                s1[j]->sum = s1[j+1]->sum;
                s1[j+1]->day = temp[0]->day;
                s1[j+1]->mou = temp[0]->mou;
                s1[j+1]->sum = temp[0]->sum;
                boo = 0;
            }
        }
        
    }
    for (int j = 1; j < i+1; j++)
    {
        printf("%d %d\n",s1[j]->mou,s1[j]->day);
    }
    
}
